package com.ruofeng.app.kotlin.common.dsl.`fun`

/**
 * Created by 邱鹏 on 2017/8/26.
 */

data class Book(var name: String? = "", var author: Author? = Author())

data class Author(var id: String = "",
                  var username: String = "default",
                  var password: String = "000000")

fun buildBook(buildAction: BookBuilder.() -> Unit): Book {
    val builder = BookBuilder()
    builder.buildAction()
    return builder.book
}

class BookBuilder {
    var book = Book()
    var name: String?
        get() = null
        set(value) {
            book.name = value
        }

    inline fun author(block: AuthorBuilder.() -> Unit) = AuthorBuilder(book).block()
}

class AuthorBuilder(var book: Book) {

    var id: String?
        get() = null
        set(value) {
            book.author!!.id = value!!
        }
    var username: String?
        get() = null
        set(value) {
            book.author!!.username = value!!
        }
    var password: String?
        get() = null
        set(value) {
            book.author!!.password = value!!
        }
}

fun main(args: Array<String>) {
    var user = buildBook {
        name = "helloWorld"
        author {
            id = "test"
            username="选择原谅"
        }
    }
    println(user)
    var book=Book().apply {
        name = "helloWorld"
        author =Author().apply {
            id = "test"
            username="选择原谅"
        }
    }
    println(book)
}